﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace CompanyRatings.SQL.PerfTest.ConsoleApp.Repositories
{
    public class DSRDWRepository
    {
        public string ConnectionString { get; set; }

        public DSRDWRepository()
            : this(ConfigurationManager.ConnectionStrings["DSRDW"].ConnectionString)
        {
        }

        public DSRDWRepository(string connectionString)
        {
            this.ConnectionString = connectionString;
        }


        // Disk only
        public DataTable GetCompanyRatings_Disk(string query)
        {
            DataTable table = new DataTable();

            using (SqlConnection conn = new SqlConnection(this.ConnectionString))
            {
                using (SqlCommand comm = new SqlCommand("sp_CompanyRatings", conn))
                {
                    comm.CommandTimeout = (int)TimeSpan.FromSeconds(60 * 60).TotalSeconds;
                    comm.CommandType = CommandType.StoredProcedure;
                    var queryParam = new SqlParameter("@query", new SqlXml(XmlReader.Create(new StringReader(query))));
                    comm.Parameters.Add(queryParam);

                    using (SqlDataAdapter adapter = new SqlDataAdapter(comm))
                    {
                        adapter.Fill(table);
                    }

                    //using (SqlDataReader reader = comm.ExecuteReader())
                    //{
                    //    // read results
                    //}
                }
            }

            return table;
        }
    }
}
